<template>
  <div class="img-list">
    <van-nav-bar
      title="图片资讯"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
      class="fixed-top"
    />
    <div class="img-list-container">
      <div class="cate">
        <ul id="cateul" ref="ulRef">
          <li>
            <span>全部</span>
          </li>
          <li v-for="item in topList" :key="item.id" @click="getImg(item.id)">
            <span>{{item.title}}</span>
          </li>
        </ul>
      </div>
      <div class="img-list-box">
        <ul>
          <li v-for="item in imgContent" :key="item.id">
            <router-link :to="`/imgs-detail/${item.id}`" >
              <img :src="item.img_url" :alt="item.title">
            </router-link>
            <p>
              <q class="title">{{item.title}}</q>
             <em style="font-size: 12px;">{{item.zhaiyao}}</em>
            </p>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
import { getImagesList, getImgList } from '@/api'
export default {
  data () {
    return {
      topList: [],
      imgContent: []
    }
  },
  created () {
    this.initList()
    this.getImg(0)
  },
  methods: {
    initList () {
      getImagesList()
        .then(res => {
          console.log(res)
          this.topList = res.data.message
          console.log(this.topList)
          let ulitWidth = 60
          let num = res.data.message.length + 1
          let ulWidth = ulitWidth * num
          this.$refs.ulRef.style.width = ulWidth + 'px'
        })
    },
    getImg (id) {
      getImgList(id)
        .then(res => {
          console.log(res)
          this.imgContent = res.data.message
        })
    }
  }
}
</script>

<style lang="scss" scoped>
  .cate {
    max-width: 100%;
    margin: 0;
    overflow-x: auto;
    overflow-y: hidden;
    font-size: 14px;
    ul {
      padding: 10px 0 10px 10px;
      margin: 5px 0;
      li {
        list-style: none;
        display: inline-block;
        padding: 0 0 0 5px;
        span {
          color: #0094ff;
        }
      }
    }
  }
  .img-list-container {
    padding: 0 8px;
  }
  .img-list-box {
    ul {
      padding: 0;
      li {
        list-style: none;
        position: relative;
      }
    }
    p {
      font-size: 14px;
      color: #fff;
      position: absolute;
      bottom: 0px;
      left: 0px;
      background-color: rgba(0, 0, 0, 0.4);
      .title {
        font-weight: bold;
      }
    }
    img {
      width: 100%;
    }
  }
</style>
